﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace DAO
{
    public class MajorDAO
    {
        public static XmlElement GetListMajorsRSS()
        {
            MemoryStream stm = new MemoryStream();
            stm = CreateRSSListMajors();

            // seek to start position
            stm.Seek(0, SeekOrigin.Begin);

            XmlTextReader xr = new XmlTextReader(stm);
            XmlDocument doc = new XmlDocument();
            doc.Load(xr);

            return doc.DocumentElement;
        }

        public static MemoryStream CreateRSSListMajors()
        {
            MemoryStream stm = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(stm, Encoding.UTF8);

            // start writing the rss tags
            writer.WriteStartDocument();
            writer.WriteStartElement("rss");
            writer.WriteAttributeString("version", "2.0");
            writer.WriteStartElement("channel");
            writer.WriteElementString("title", "ListMajor - Rongcon.com");
            writer.WriteElementString("link", "http://localhost:8080/itemService/ListMajor.rss");
            writer.WriteElementString("description", "I'm RongCon");
            writer.WriteElementString("copyright", "Copyright by RongCon!");

            List<ENROLLMAJOR> lstMajor = GetListMajor();

            if (lstMajor != null)
            {
                for (int i = 0; i < lstMajor.Count; i++)
                {
                    writer.WriteStartElement("item");
                    writer.WriteElementString("ID", lstMajor[i].ID.ToString());
                    writer.WriteElementString("MajorName", lstMajor[i].MajorName);
                    writer.WriteElementString("MajorDescription", lstMajor[i].MajorDescription);
                    writer.WriteEndElement();
                }
            }

            // Close all tags
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();

            return stm;
        }

        public static List<ENROLLMAJOR> GetListMajor()
        {
            FindJobDBDataContext db = new FindJobDBDataContext();
            List<ENROLLMAJOR> result = new List<ENROLLMAJOR>();

            var query = from n in db.ENROLLMAJORs
                        select n;
             
            foreach (ENROLLMAJOR r in query)
                result.Add(r);

            return result;
        }
    }
}
